//
//  ViewController.m
//  HomeWork-22
//
//  Created by 鹏飞罗 on 16/8/12.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "ESRootClass.h"
#import "Data.h"
#import "Contents.h"
#import "Mvlist.h"
#import "List.h"
#import "Mvlist.h"
#import "Singers.h"
#import "Mv_Show_Play.h"
#import "PFCycleImages.h"
#import "MyTableViewCell.h"
#define kTableViewCell @"reuse"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UIScrollView *scroview;
@property (nonatomic, strong) PFCycleImages *cycleImage;
@property (nonatomic, strong) ESRootClass *rootClass;
@property (nonatomic, strong) NSMutableArray *mvShowPlayArray;
@property (nonatomic, strong) NSMutableArray<List *> *listArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self handleDate];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.00 green:0.80 blue:0.50 alpha:1.00];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButton)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sort"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButton)];
    [self createSegment];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 44) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:kTableViewCell];
    [self.view addSubview:_myTableView];
    
    [self createHeadView];
}
- (void) leftButton {
    
}
- (void) rightButton {
    
}
#pragma mark - tableViewCell
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cellHeight = CGRectGetHeight(self.view.frame) ;
    return cellHeight;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
    cell.list = _listArray[indexPath.row];
    return cell;
}
#pragma mark - tableViewHeadView
- (void) createHeadView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 150)];
//    headView.backgroundColor = [UIColor redColor];
    self.myTableView.tableHeaderView = headView;
    self.cycleImage = [[PFCycleImages alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(headView.frame), CGRectGetHeight(headView.bounds) * 3 / 4)];
    self.cycleImage.backgroundColor = [UIColor yellowColor];
    [headView addSubview:self.cycleImage];        
    UIButton *hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat height = CGRectGetHeight(headView.bounds);
    CGFloat width = CGRectGetWidth(headView.frame);
    hotButton.frame = CGRectMake(0, height * 3 / 4, width / 3, height / 4);
    [hotButton setImage:[UIImage imageNamed:@"paihang"] forState:UIControlStateNormal];
    [hotButton setTitle:@"排行" forState:UIControlStateNormal];
    [hotButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [headView addSubview:hotButton];
    
    UIButton *spotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    spotButton.frame = CGRectMake(width / 3 , height * 3 / 4, width / 3, height / 4);
    [spotButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [spotButton setTitle:@"现场" forState:UIControlStateNormal];
    [spotButton setImage:[UIImage imageNamed:@"xianchang"] forState:UIControlStateNormal];
    [headView addSubview:spotButton];
    
    UIButton *AXNButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    AXNButton.frame = CGRectMake(width * 2 / 3, height * 3 / 4, width / 3, height / 4);
    [AXNButton setTitle:@"频道" forState:UIControlStateNormal];
    [AXNButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [AXNButton setImage:[UIImage imageNamed:@"pindao"] forState:UIControlStateNormal];
    [headView addSubview:AXNButton];
}

#pragma mark - handleDate
- (void) handleDate {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 90;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", @"application/x-javascript", nil];
    NSString *requestURLString = @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294";
    
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.rootClass = [ESRootClass modelWithDictionary:responseObject];
        _mvShowPlayArray = [NSMutableArray array];
        _mvShowPlayArray = [NSMutableArray arrayWithArray:self.rootClass.data.mv_show_play];
        Mv_Show_Play *firstObject = [_mvShowPlayArray firstObject];
        Mv_Show_Play *lastObject = [_mvShowPlayArray lastObject];
        [_mvShowPlayArray insertObject:lastObject atIndex:0];
        [_mvShowPlayArray addObject:firstObject];
        NSMutableArray<NSString *> *imagArray = [NSMutableArray array];
        for (int i = 0; i < _mvShowPlayArray.count; i++) {
            Mv_Show_Play *object = _mvShowPlayArray[i];
            NSString *imageString = object.picurl;
            [imagArray addObject:imageString];
        }
        self.cycleImage.array = imagArray;
        
        _listArray = [NSMutableArray array];
        self.listArray = [NSMutableArray arrayWithArray:self.rootClass.data.contents.list];
        
//            for (int i = 0; i < _mvShowPlayArray.count; i++) {
//                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.scroview.frame), CGRectGetHeight(self.scroview.frame))];
//                Mv_Show_Play *mvShowPlay = _mvShowPlayArray[i];
//                NSString *urlString = mvShowPlay.picurl;
//                NSURL *picURL = [NSURL URLWithString:urlString];
//                imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:picURL]];
//                NSLog(@"%@",urlString);
//                [self.scroview addSubview:imageView];
//            }
//            self.scroview.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)* _mvShowPlayArray.count, CGRectGetHeight(self.scroview.frame));
//            self.scroview.contentOffset = CGPointMake(CGRectGetWidth(self.view.frame), 0);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog( @"error --- %@", error);
    }];
}
- (void)setListArray:(NSMutableArray *)listArray {
    _listArray = listArray;
    [self.myTableView reloadData];
}
#pragma mark - segment
- (void) createSegment {
//    titileSegment
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"我的", @"音乐馆", @"发现"]];
    segment.tintColor = [UIColor clearColor];
    [segment setWidth:80 forSegmentAtIndex:1];
    // 正常状态下
    NSDictionary * normalTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:16.0f],NSForegroundColorAttributeName : [UIColor whiteColor]};
    [segment setTitleTextAttributes:normalTextAttributes forState:UIControlStateNormal];
    
    // 选中状态下
    NSDictionary * selctedTextAttributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:20.0f],NSForegroundColorAttributeName : [UIColor whiteColor]};
    [segment setTitleTextAttributes:selctedTextAttributes forState:UIControlStateSelected];
    self.navigationItem.titleView = segment;
    
    //    titileSegment
    UISegmentedControl *titleSegment = [[UISegmentedControl alloc] initWithItems:@[@"精选", @"排行", @"歌单", @"电台", @"MV"]];
    titleSegment.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44);
    titleSegment.tintColor = [UIColor clearColor];
    NSDictionary * normalTextAttributes1 = @{NSFontAttributeName : [UIFont systemFontOfSize:16.0f],NSForegroundColorAttributeName : [UIColor blackColor]};
    [titleSegment setTitleTextAttributes:normalTextAttributes1 forState:UIControlStateNormal];
    
    // 选中状态下
    NSDictionary * selctedTextAttributes1 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:20.0f],NSForegroundColorAttributeName :[UIColor colorWithRed:0.00 green:0.84 blue:0.50 alpha:1.00] };
    [titleSegment setTitleTextAttributes:selctedTextAttributes1 forState:UIControlStateSelected];
    [self.view addSubview:titleSegment];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
